package api_servers
{
	import UIFree.ObjectEvent;
	import UIFree.uif_app;
	
	import flash.net.URLVariables;
	
	public class pjf_vkeserver extends api_externalserver {
		public static const VK_ERROR: String = 'VK_ERROR';
		
		public function pjf_vkeserver(a_sroot: uif_app) {
			var a_vars: Object = Object(a_sroot.stage).loaderInfo.parameters;
			super(a_sroot, a_vars['api_url'], a_vars['viewer_id'], a_vars['api_id'], 
					secrets[a_vars['api_id']], 200);
			vars = a_vars;
			vars['ver'] = 1;
		}
		
		public function get secrets(): Object {
			return {
				1895079: 'epkdhkCQnI'
			}
		} 

/*		override public function createRequest(a_vars: Array, a_url: String='', method: String = URLRequestMethod.POST): URLRequest {
			return super.createRequest(a_vars, a_url, URLRequestMethod.GET);
		}
*/		

		override public function convertVars(): Object {
			return {
				'photo': {name:'pic'},
				'photo_medium': {name:'pic'},
				'photo_big': {name:'pic_big'},
				'src_big': {name:'src'},
				'src_xbig': {name:'src'},
				'src_xxbig': {name:'src'}
			}			
		}
		
		override protected function srHelper( arr:Array): URLVariables {// sendRequestHelper
			arr.push( {pn: "api_id", pv: app_id } );
			arr.push( {pn: "v", pv: '2.0' } );
			arr.push( {pn: "format", pv: 'JSON' } );
					  
			arr.sortOn('pn');
					  
			var url_vars:URLVariables = new URLVariables();
			for( var i: int = 0; i < arr.length; i++ ) {
				url_vars[ (arr[i].pn) ] = arr[ i ].pv;
			}
					  
			url_vars["sig"] = makeSig( arr );
			return url_vars;
		}
	    
	    override protected function prepareData(a_queryID:String, data:*):* {
	    	var error: Object = data['error']?data['error']:
	    							(data.response['error']?data.response['error']:
	    								(data.response[0]?data.response[0]['error']:null));
	    	if (error) {
	    		if (error.error_code == 7)
	    			error.error_msg = 'Нет доступа, действие запрещено пользователем';
	    		error.queryID = a_queryID;
	    		sroot.dispatchEvent(new ObjectEvent(VK_ERROR, error));
	    		return null;
	    	} else return super.prepareData(a_queryID, data.response);
	    }

		public function getAlbums(uid: String, handler_:*, queryID:String='getAlbums'): void {
	    	jsrHelper([
						{pn: "method", pv: "photos.getAlbums"},
						{pn: "uid", pv: uid}
	    			], handler_, queryID, true);
		}
		
		public function getPhotos(uid: String, aid: String, handler_:*, queryID:String='getPhotos'): void {
	    	jsrHelper([
						{pn: "method", pv: "photos.get"},
						{pn: "uid", pv: uid},
						{pn: "aid", pv: aid}
	    			], handler_, queryID + aid, true);
		}
		
	    public function getAllFriends(uid: String, handler_:*, queryID:String='getAllFriends'): void {
	    	var appUsers: Array;
	    	var friends: Array;
	    	function AFResponse(): void {
	    		if (appUsers && friends) {
    				for (var i:int=0; i<friends.length; i++)
    					friends[i].isInstall = (friends[i].uid in appUsers); 
	    			handler_(friends);
	    		}
	    	}
	    	jsrHelper([{pn: "method", pv: "friends.getAppUsers"}], function(data: Object): void {
	    		appUsers = (data.length>0)?(data as Array):[];
	    		AFResponse();
	    	}, 'getAppUsers', true);
	    	jsrHelper([
						{pn: "method", pv: "execute"},
						{pn: "code", pv: 'var uids=API.getFriends();return API.getProfiles({"uids":uids, "fields":"uid,first_name,last_name,nickname,sex,bdate,photo,photo_medium,photo_big,contacts"});'}
	    			], function(data: Object): void {
	    				var buff: Array = new Array();
	    				for (var i:int=0; i<data.length; i++)
	    					if (data[i]) buff.push(data[i]);
	    				friends = _cache['friends'] = buff;
	    				AFResponse();
	    			}, 'friends', true);
	    }
	    
	    override public function getFriends(uid: String, handler_:*, queryID:String='getFriends'): void {
	    	jsrHelper([
						{pn: "method", pv: "getAppFriends"}
	    			], function(data: Object): void {
	    				handler_(data.length?data:[]);
	    			}, queryID, true);
	    }
	    
	    override public function getProfiles(a_uids: String, handler_:*, fields: String='', queryID:String=''): void {
	    	jsrHelper([
						{pn: "method", pv: "getProfiles"},
						{pn: 'uids', pv: a_uids},
						{pn: 'fields', pv: 'uid,first_name,last_name,nickname,sex,bdate,photo,photo_medium,photo_big,contacts'}
	    			], handler_, queryID);
	    }
	    
	    public function getPhotoUploadServer(_handle: Function): void {
	    	jsrHelper([
						{pn: "method", pv: "wall.getPhotoUploadServer"}
	    			], _handle, 'getPhotoUploadServer', true);
	    }
	    
/*	    
	    public function publicWallPost(uids: Array, image_url: String, message: String, queryID:String='publicWallPost'): void {
			var fr:FileReference = new FileReference();
			fr.download(new URLRequest(image_url));
			
	    	jsrHelper([
						{pn: "method", pv: "wall.getPhotoUploadServer"}
	    			], function(data: *): void {
						var ldr: URLLoader = new URLLoader();
						var request: URLRequest = new URLRequest(data.upload_url);
						request.method = URLRequestMethod.POST;
						var vars: URLVariables = new URLVariables();
						vars.photo = image_url;
						request.data = vars;
						ldr.addEventListener(Event.COMPLETE, function(e: Event): void {
							trace(e);
						});
						ldr.load(request);
						
	    			}, queryID);
	    }*/
	    
	    public function savePost(uid: String, send_id: uint, uploadResult: Object, message: String, _handle: Function=null): void {
	    	jsrHelper([
						{pn: "method", pv: "wall.savePost"},
						{pn: "wall_id", pv: uid},
						{pn: "post_id", pv: send_id},
						{pn: "server", pv: uploadResult.server},
						{pn: "photo", pv: uploadResult.photo},
						{pn: "hash", pv: uploadResult.hash},
						{pn: "photo_id", pv: uid + '_' + send_id},
						{pn: "message", pv: message},
	    			], _handle, 'savePost');
	    }	    
	}
}